package ivan.bookKeeper;

import java.util.concurrent.ExecutionException;

import so.library.AddLibrary;
import so.library.ReturnAllLibraries;
import domain.Library;
import android.app.Activity;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LibraryCardTest extends Activity implements OnClickListener {
	private static final String TAG = LibraryCardTest.class.getSimpleName();
	private EditText etLibraryName;
	private EditText etLibraryAddress;
	private EditText etLibraryEmail;
	private EditText etLibraryPhone;
	private EditText etLibraryBorrowLength;
	private EditText etLibraryMaxNumExtending;
	private Button btnAddLibrary;

	private TextView txtLibraryName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		etLibraryName = (EditText) findViewById(R.id.libraryName);
		etLibraryAddress = (EditText) findViewById(R.id.libraryAddress);
		etLibraryEmail = (EditText) findViewById(R.id.libraryEmail);
		etLibraryPhone = (EditText) findViewById(R.id.libraryPhone);
		etLibraryBorrowLength = (EditText) findViewById(R.id.libraryBorrowLength);
		etLibraryMaxNumExtending = (EditText) findViewById(R.id.libraryMaxNumExtending);
		btnAddLibrary = (Button) findViewById(R.id.buttonAddLibrary);

		btnAddLibrary.setOnClickListener(this);

		txtLibraryName = (TextView) findViewById(R.id.txtLibraryName);
		txtLibraryName.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(LibraryCardTest.this, "hej hej hej!!",
						Toast.LENGTH_LONG).show();
			}
		});

		// ///////////////// Testing fetchAll
		Library library = new Library();
		AsyncTask<Object, Object, Object> a = new ReturnAllLibraries(
				LibraryCardTest.this).execute(library);
		Cursor cursor = null;
		try {
			cursor = (Cursor) a.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		while (cursor.moveToNext()) {
			String col = cursor.getInt(0) + cursor.getString(3)
					+ cursor.getString(4);
			Log.d(TAG, col);
		}
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		String libName = etLibraryName.getText().toString().trim();
		String libAddress = etLibraryAddress.getText().toString().trim();
		String libEmail = etLibraryEmail.getText().toString().trim();
		String libPhone = etLibraryPhone.getText().toString().trim();
		int libBorrowLength = Integer.valueOf(etLibraryBorrowLength.getText()
				.toString().trim());
		int libMaxNumExtending = Integer.valueOf(etLibraryMaxNumExtending
				.getText().toString().trim());

		Library library = new Library();
		library.setLibraryName(libName);
		library.setLibraryAddress(libAddress);
		library.setLibraryEmail(libEmail);
		library.setLibraryPhone(libPhone);
		library.setLibraryPhone(libPhone);
		library.setLibraryPhone(libPhone);
		library.setBorrowLength(libBorrowLength);
		library.setMaxNumExtending(libMaxNumExtending);
		Log.d(TAG, "-------CREATED LIBRARY: " + library.toString());

		new AddLibrary(LibraryCardTest.this).execute(library);

	}
}
